//
//  ApplyDetail.swift
//  Employee
//
//  Created by swordjoy on 2017/6/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON

struct ApplyDetail {
  let title: String
  let number: String
  let result: [InspectionProject]
  let approverStatus: String
  let images: [FlowImage]
  let typeName: String
  let sortId: String
  let createStaffId: String
  let people: [CheckPeopleState]
  let approvaled: Bool
  let withdrawed: Bool
  
  var imageUrlStrings: [String] {
    get {
      var temps: [String] = []
      for item in images {
        temps.append(item.filePath)
      }
      return temps
    }
  }
  
  init(json: JSON) {
    title = json["title"].stringValue
    number = json["number"].stringValue
    approverStatus = json["approverStatus"].stringValue
    self.typeName = json["typeName"].stringValue
    self.sortId = json["sortId"].stringValue
    self.createStaffId = json["createStaffId"].stringValue
    
    let jsonString = json["result"].stringValue
    
    let data = jsonString.data(using: .utf8)
    if let temp = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers) {
      let json = JSON.init(temp)
      self.result = InspectionProject.array(from: json.arrayValue)
    }
    else { self.result = [] }
    self.images = FlowImage.array(from: json["filesDtos"].arrayValue)
    
    self.people = CheckPeopleState.array(from: json["flowSortDtos"].arrayValue)
    self.withdrawed = json["withdrawed"].boolValue
    self.approvaled = json["approvaled"].boolValue
  }
}

struct CheckPeopleState: ModelProtocol {
  let approvalName: String
  let approvalStaffAvatar: String
  let approverStatus: String
  let approverExplain: String
  let approverTime: String
  let flowSortId: String
  var isCheck = false
  
  init(json: JSON) {
    self.approvalName = json["approvalName"].stringValue
    self.approvalStaffAvatar = json["approvalStaffAvatar"].stringValue
    self.approverStatus = json["approverStatus"].stringValue
    self.approverExplain = json["approverExplain"].stringValue
    self.approverTime = json["approverTime"].stringValue
    self.flowSortId = json["flowSortId"].stringValue
  }
  
  static func array(from jsonArray: [JSON]) -> [CheckPeopleState] {
    return jsonArray.map({ CheckPeopleState(json: $0) })
  }
}
